package com.cc.exception.resolver;

import com.cc.exception.CustomException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:  统一异常处理方式一：定义异常处理解析类
 * @author: 新梦想.陈超
 * @date:  2022/5/4 下午6:26
 * @version: v1.0
 **/
//@Component
public class CustomExceptionResolver implements HandlerExceptionResolver {
	@Override
	public ModelAndView resolveException(HttpServletRequest request,
										 HttpServletResponse response, Object handler, Exception ex) {
		ex.printStackTrace();
		CustomException customException = null;
		//如果抛出的是系统自定义异常则直接转换
		if(ex instanceof CustomException){
			customException = (CustomException)ex;
			//其他处理：1.记录日志  2，发邮件  3，发短信  
		}else{
			//如果抛出的不是系统自定义异常则重新构造一个未知错误异常。
			customException = new CustomException("未知错误，请与系统管理 员联系！");
			//其他处理：1.记录日志  2，发邮件  3，发短信  
		}
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("message", customException.getMessage());
		modelAndView.setViewName("50x");
		return modelAndView;
	}
}